<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="imgUrl">
      <el-input v-model="dataForm.imgUrl" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="加入时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="加入时间"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="所在地" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="所在地"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="attress">
      <el-input v-model="dataForm.attress" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="money">
      <el-input v-model="dataForm.money" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="point">
      <el-input v-model="dataForm.point" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="姓名"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          nickname: '',
          password: '',
          imgUrl: '',
          phone: '',
          createTime: '',
          idCard: '',
          cid: '',
          attress: '',
          money: '',
          point: '',
          userName: ''
        },
        dataRule: {
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          imgUrl: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '加入时间不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '所在地不能为空', trigger: 'blur' }
          ],
          attress: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ],
          point: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/consumption/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.nickname = data.user.nickname
                this.dataForm.password = data.user.password
                this.dataForm.imgUrl = data.user.imgUrl
                this.dataForm.phone = data.user.phone
                this.dataForm.createTime = data.user.createTime
                this.dataForm.idCard = data.user.idCard
                this.dataForm.cid = data.user.cid
                this.dataForm.attress = data.user.attress
                this.dataForm.money = data.user.money
                this.dataForm.point = data.user.point
                this.dataForm.userName = data.user.userName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/consumption/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'nickname': this.dataForm.nickname,
                'password': this.dataForm.password,
                'imgUrl': this.dataForm.imgUrl,
                'phone': this.dataForm.phone,
                'createTime': this.dataForm.createTime,
                'idCard': this.dataForm.idCard,
                'cid': this.dataForm.cid,
                'attress': this.dataForm.attress,
                'money': this.dataForm.money,
                'point': this.dataForm.point,
                'userName': this.dataForm.userName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
